home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / DevTools / eText5 / Source / unused4Code / BBC.bproj / BilliardView.m < prev    next >
Encoding:
Text File  |  1994-07-08  |  4.4 KB  |  169 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //    FILENAME:    BilliardView.m 
  3. //    SUMMARY:    Implementation of a Billiard-Ball View
  4. //    SUPERCLASS:    View
  5. //    INTERFACE:    None
  6. //    PROTOCOLS:    None
  7. //    AUTHOR:        Rohit Khare
  8. //    COPYRIGHT:    (c) 1994 California Institure of Technology, eText Project
  9. //    COPYRIGHT:    (c) 1992 Rohit Khare (core simulation code)
  10. ///////////////////////////////////////////////////////////////////////////////
  11. //    DESCRIPTION
  12. //        Separated out view from old BilliardView code.
  13. ///////////////////////////////////////////////////////////////////////////////
  14. //    HISTORY
  15. //    05/29/94:    Created as an eText Annotation.
  16. //    1992-93:    Developed for CS20ab.
  17. ///////////////////////////////////////////////////////////////////////////////
  18.  
  19. #import "BBC.h"
  20.  
  21. @implementation BilliardView
  22. - fillCache
  23. {
  24.     int i,j,left,bottom;
  25.     NXPoint pt;
  26.  
  27.     [cacheGrid lockFocus];
  28.     bottom = left = 0;
  29.     i = y;
  30.     while(i-- > 0){
  31.         j = -1;
  32.         while(++j < x){
  33.             pt.x = left; pt.y = bottom;
  34.             [Grid composite:NX_COPY toPoint:&pt];
  35.             left += 20;
  36.         }
  37.         left = 0;
  38.         bottom += 20;
  39.     }
  40.     [cacheGrid unlockFocus];
  41.     return self;
  42. }
  43.  
  44. - initFrame:(const NXRect *)nf
  45. {    NXSize sz;
  46.  
  47.     [super initFrame:nf];
  48.     Mucus = [NXImage findImageNamed:"Trail"];
  49.     Wall  = [NXImage findImageNamed:"Wall"];
  50.     Grid  = [NXImage findImageNamed:"Grid"];
  51.     N = [NXImage findImageNamed:"N"];
  52.     S = [NXImage findImageNamed:"S"];
  53.     E = [NXImage findImageNamed:"E"];
  54.     W = [NXImage findImageNamed:"W"];
  55.     x = y = 10; sz.width = x * 20.0; sz.height = y * 20.0;
  56.     cacheGrid = [[NXImage alloc] initSize:&sz];
  57.     [cacheGrid setUnique:YES];    // this image must stay opaque!
  58.     [cacheGrid setBackgroundColor:NX_COLORWHITE];
  59.     [cacheGrid useCacheWithDepth:NX_DefaultDepth];
  60.     [self fillCache];
  61.     return self;
  62. }
  63. - setBBC:newBBC {theBBC = newBBC; return self;}
  64. - setRes:(int) hres :(int) vres 
  65. {    NXSize sz;
  66.  
  67.     //if ((x != hres) || (y != vres)) {
  68.     x = hres;     y = vres; 
  69.     sz.width = x * 20.0; sz.height = y * 20.0;
  70.     [cacheGrid setSize:&sz];
  71.     [self fillCache];
  72.     [self sizeTo:(x * 20.0) :(y * 20.0)];
  73.     [self display];
  74.     //}
  75.     return self;
  76. }
  77. - drawSelf:(const NXRect *)rects :(int)rectCount {
  78.     int i,j,left,bottom;
  79.     char dir[2];
  80.     NXPoint pt;
  81.     char **state = [theBBC state];
  82. //    BOOL isAccelerated = [theBBC isAccelerated];
  83.     BOOL mucusMode = [theBBC doesShowPath];
  84.  
  85.     if (state == NULL) {return self;}
  86.     if ([self isFocusView]) {
  87.     [cacheGrid composite:NX_COPY toPoint:&(bounds.origin)];
  88.     dir[1] = '\0';
  89.     bottom = left = 0;
  90.     i = y;
  91.     while(i-- > 0){
  92.         j = -1;
  93.         while(++j < x){
  94.             pt.x = left; pt.y = bottom;
  95.             switch (state[i][j]) {    
  96.                 case ';':{    if(mucusMode) 
  97.                                 [Mucus composite:NX_SOVER toPoint:&pt];
  98.                             break;}
  99.                 case 'O':{    [Wall composite:NX_SOVER toPoint:&pt];
  100.                             break;}
  101.                 case 'N':{    [N composite:NX_SOVER toPoint:&pt];
  102.                             break;}
  103.                 case 'S':{    [S composite:NX_SOVER toPoint:&pt];
  104.                             break;}
  105.                 case 'W':{    [W composite:NX_SOVER toPoint:&pt];
  106.                             break;}
  107.                 case 'E':{    [E composite:NX_SOVER toPoint:&pt];
  108.                             break;}
  109.             }
  110.             left += 20;
  111.         }
  112.         left = 0;
  113.         bottom += 20;
  114.     }
  115.     }
  116.     return self;
  117. }
  118.  
  119. - mouseDown: (NXEvent *)ptr
  120. {
  121.     NXPoint    mloc,pt; int i,j; NXRect boundsy,sq;
  122.     char **state = [theBBC state];
  123.     
  124.     if ((![[[NXApp inspector] currentObj] isKindOf:[BBCUI class]])  ||
  125.         ([[[NXApp inspector] currentObj] bbc] != theBBC)) {
  126.         [[NXApp inspector] inspect:[[BBCUI new] setBBC:theBBC]];
  127.         return self;
  128.     }
  129.     if (![theBBC isEditable]) return self;
  130.     [self lockFocus];
  131.     mloc = ptr->location;
  132.     [self convertPoint:&mloc fromView:nil];
  133.     [self getBounds:&boundsy];
  134.     j = (int) floor((mloc.x / boundsy.size.width) * x);
  135.     i = y - (int) floor((mloc.y / boundsy.size.height) * y);
  136.     i--;
  137.     if (i < 0) i = 0;
  138.     pt.x = j * 20.0;
  139.     pt.y = boundsy.size.height -  (i+1) * 20.0;
  140.     if ((i <= y) && (j <= x)){ 
  141.         switch (state[i][j]) {
  142.             case ';':
  143.             case '.':{    state[i][j] = 'O';
  144.                         [Wall composite:NX_COPY toPoint:&pt];
  145.                             break;}
  146.             case 'O':{    state[i][j] = 'N';
  147.                         [N composite:NX_COPY toPoint:&pt];
  148.                             break;}
  149.             case 'N':{    state[i][j] = 'S';
  150.                         [S composite:NX_COPY toPoint:&pt];
  151.                             break;}
  152.             case 'S':{    state[i][j] = 'W';
  153.                         [W composite:NX_COPY toPoint:&pt];
  154.                             break;}
  155.             case 'W':{    state[i][j] = 'E';
  156.                         [E composite:NX_COPY toPoint:&pt];
  157.                             break;}
  158.             case 'E':{    state[i][j] = '.';
  159.                         sq.origin = pt; sq.size.height = sq.size.width = 20.0;
  160.                         PSsetgray(NX_WHITE);
  161.                         NXRectFill(&sq);
  162.                             break;}
  163.         }
  164.     }
  165.     [self unlockFocus];
  166.     [[self window] flushWindow];
  167.     return self;
  168. }
  169. @end